
package com.carkeeper.servcieManagement.service.impl;

import com.carkeeper.common.utils.IdWorker;
import com.carkeeper.servcieManagement.dao.ServiceMapper;
import com.carkeeper.servcieManagement.dao.ServiceTypeMapper;
import com.carkeeper.servcieManagement.service.ServiceTypeService;
import com.carkeeper.serviceManagement.pojo.ServiceType;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ServiceTypeServiceImpl implements ServiceTypeService {

    @Autowired
    private ServiceTypeMapper serviceTypeMapper;
    @Autowired
    private ServiceMapper ServiceMapper;
    @Autowired
    private IdWorker idWorker;


    /**
     * 查询全部列表
     *
     * @return
     */
    @Override
    public List<ServiceType> findAll() {
        return serviceTypeMapper.selectAll();
    }

    /**
     * 条件+分页查询
     *
     * @param searchMap 查询条件
     * @param page      页码
     * @param size      页大小
     * @return 分页结果
     * @Override
     */

    public Page<ServiceType> findPage(Map<String, Object> searchMap, int page, int size) {
        PageHelper.startPage(page, size);
        Example example = createExample(searchMap);
        return (Page<ServiceType>) serviceTypeMapper.selectByExample(example);
    }

    /**
     * 删除
     */
    @Override
    public int delete(Long id) {
        //判断在serviceType表中是否有parentId与此id相同
        List<ServiceType> serviceTypes = serviceTypeMapper.selectAll();
        for (ServiceType serviceType : serviceTypes) {
            if (id == serviceType.getParentId()) {
                return 0;
            }
        }
        //判断在service中是否有type_2或type_1与此id相同
        List<com.carkeeper.serviceManagement.pojo.Service> services = ServiceMapper.selectAll();
        for (com.carkeeper.serviceManagement.pojo.Service service : services) {
            if (service.getType_1() == id || service.getType_2() == id) {
                return 1;
            }
        }
        return serviceTypeMapper.deleteByPrimaryKey(id);
    }


    /**
     * 增加
     *
     * @param serviceType
     */

    @Override
    public void add(ServiceType serviceType) {
        Long id = idWorker.nextId();
        serviceType.setId(id);//设置主键
        serviceTypeMapper.insert(serviceType);
    }


    /**
     * 修改
     *
     * @param serviceType
     */

    @Override
    public void update(ServiceType serviceType) {
        serviceTypeMapper.updateByPrimaryKey(serviceType);
    }


    /**
     * 根据ID查询
     *
     * @param id
     * @return
     *//*
     */
    @Override
    public ServiceType findById(Long id) {

        return serviceTypeMapper.selectByPrimaryKey(id);
    }

    //查询所有一级菜单
    @Override
    public List<ServiceType> findLevel1() {
        Map searchMap = new HashMap<String, Object>();
        searchMap.put("level", 1);
        Example example = createExample(searchMap);
        return (List<ServiceType>) serviceTypeMapper.selectByExample(example);

    }

    //根据一级分类id查询所有二级分类
    @Override
    public List<ServiceType> findLevel2(Long id) {
        Map searchMap = new HashMap<String, Object>();
        searchMap.put("parentId", id);
        Example example = createExample(searchMap);
        return (List<ServiceType>) serviceTypeMapper.selectByExample(example);
    }


    /**
     * 构建查询对象
     *
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap) {
        Example example = new Example(ServiceType.class);
        Example.Criteria criteria = example.createCriteria();
        if (searchMap != null) {
            // code
            if (searchMap.get("code") != null && !"".equals(searchMap.get("code"))) {
                criteria.andEqualTo("code", searchMap.get("code"));
            }
            // 分类名称
            if (searchMap.get("name") != null && !"".equals(searchMap.get("name"))) {
                criteria.andLike("name", "%" + searchMap.get("name") + "%");
            }
            // parentId
            if (searchMap.get("parentId") != null && !"".equals(searchMap.get("parentId"))) {
                criteria.andEqualTo("parentId", searchMap.get("parentId"));
            }
            if (searchMap.get("level") != null && !"".equals(searchMap.get("level"))) {
                criteria.andEqualTo("level", searchMap.get("level"));
            }
        }
        return example;
    }


}

